<template>
  <el-main>
    <el-form size="small">
      <el-form-item>
        <el-button
          v-if="hasPerm('sys:menu:add')"
          type="primary"
          icon="el-icon-plus"
          @click="addMenu"
        >新增</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :height="tableHeight"
      :data="tableList"
      row-key="menuId"
      :tree-props="{ children: 'children' }"
      border
      stripe
      default-expand-all
    >
      <el-table-column prop="menuLabel" label="菜单名称" />
      <el-table-column prop="type" label="菜单类型">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.type == '0'">目录</el-tag>
          <el-tag v-if="scope.row.type == '1'" type="success">菜单</el-tag>
          <el-tag v-if="scope.row.type == '2'" type="danger">按钮</el-tag>
        </template>
      </el-table-column>

      <el-table-column prop="icon" label="菜单图标">
        <template slot-scope="scope">
          <i :class="scope.row.icon" />
        </template>
      </el-table-column>

      <el-table-column prop="name" label="路由名称" />

      <el-table-column prop="path" label="路由地址" />

      <el-table-column prop="url" label="组件路径" />
      <el-table-column prop="menuCode" label="权限字段" />
      <el-table-column align="center" width="200" label="操作">
        <template slot-scope="scope">
          <el-button
            v-if="hasPerm('sys:menu:edit')"
            type="primary"
            icon="el-icon-edit"
            size="small"
            @click="editMenu(scope.row)"
          >编辑</el-button>
          <el-button
            v-if="hasPerm('sys:menu:delete')"
            type="danger"
            icon="el-icon-delete"
            size="small"
            @click="deleteMenu(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 新增或编辑弹框 -->
    <sys-dialog
      :title="dialog.title"
      :height="dialog.height"
      :width="dialog.width"
      :visible="dialog.visible"
      @onConfirm="onConfirm"
      @onClose="onClose"
    >
      <div slot="content">
        <el-form
          ref="addForm"
          style="margin-left: 40px"
          :model="addModel"
          :rules="rules"
          label-width="80px"
          :inline="true"
          size="normal"
        >
          <el-row>
            <el-col :span="24">
              <el-form-item prop="type" label="菜单类型">
                <el-radio-group v-model="addModel.type">
                  <el-radio :label="'0'">目录</el-radio>
                  <el-radio :label="'1'">菜单</el-radio>
                  <el-radio :label="'2'">按钮</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item prop="parentName" label="上级菜单">
            <el-input
              v-model="addModel.parentName"
              size="small"
              @click.native="selectParent"
            />
          </el-form-item>
          <el-form-item prop="menuLabel" label="菜单名称">
            <el-input
              v-model="addModel.menuLabel"
              size="small"
            />
          </el-form-item>
          <el-form-item
            prop="icon"
            label="菜单图标"
          >
            <el-input
              v-model="addModel.icon"
              size="small"
            />
          </el-form-item>
          <el-form-item v-if="addModel.type == '1'" prop="name" label="路由名称">
            <el-input
              v-model="addModel.name"
              size="small"
            />
          </el-form-item>
          <el-form-item v-if="addModel.type != '2'" prop="path" label="路由地址">
            <el-input
              v-model="addModel.path"
              size="small"
            />
          </el-form-item>
          <el-form-item v-if="addModel.type == '1'" prop="url" label="组件路径">
            <el-input
              v-model="addModel.url"
              size="small"
            />
          </el-form-item>
          <el-form-item prop="menuCode" label="权限字段">
            <el-input
              v-model="addModel.menuCode"
              size="small"
            />
          </el-form-item>
          <el-form-item label="权限备注">
            <el-input
              v-model="addModel.remark"
              size="small"
            />
          </el-form-item>
          <el-form-item label="权限序号">
            <el-input
              v-model="addModel.orderNum"
              size="small"
            />
          </el-form-item>
        </el-form>
      </div>
    </sys-dialog>
    <!-- 选择上级部门菜单 -->
    <sys-dialog
      :title="parentDialog.title"
      :height="parentDialog.height"
      :width="parentDialog.width"
      :visible="parentDialog.visible"
      @onClose="onParentClose"
      @onConfirm="onParentConfirm"
    >
      <div slot="content">
        <el-tree
          ref="parentTree"
          :data="parentList"
          node-key="menuId"
          :props="parentProps"
          empty-text="暂无数据"
          :show-checkbox="false"
          default-expand-all
          :highlight-current="true"
          :expand-on-click-node="false"
          @node-click="treeClick"
        >
          <div slot-scope="{ node, data }">
            <!-- 如果没有下级，就是最好一层 -->
            <span v-if="data.children.length == 0">
              <i style="margin-right: 3px" class="el-icon-document" />
            </span>
            <!-- 有下级，需要判断是展开还是关闭 -->
            <span v-else @click.stop="openBtn(data)">
              <i
                v-if="data.open"
                style="margin-right: 3px"
                class="iconfont icon-jia"
              />
              <i v-else style="margin-right: 3px" class="iconfont icon-jian" />
            </span>
            <!-- 树节点的名称 -->
            <span style="margin-left: 3px">{{ node.label }}</span>
          </div>
        </el-tree>
      </div>
    </sys-dialog>
  </el-main>
</template>

<script>
import { getMenuListApi, getParentTreeApi, addMenuApi, editMenuApi, deleteMenuApi } from '@/api/menu'
import SysDialog from '@/components/system/SysDialog'
export default {
  components: {
    SysDialog
  },
  data() {
    return {
      // 表格高度
      tableHeight: 0,
      // 表格数据
      tableList: [],
      selectNode: {
        menuId: '',
        menuLabel: ''
      },
      // 上级菜单树属性配置
      parentProps: {
        children: 'children',
        label: 'menuLabel'
      },
      // 上级菜单数据
      parentList: [],
      // 上级菜单弹框属性
      parentDialog: {
        title: '选择上级菜单',
        width: 300,
        height: 450,
        visible: false
      },
      // 表单验证规则
      rules: {
        type: [
          {
            required: true,
            trigger: 'change',
            message: '请选择菜单类型'
          }
        ],
        parentName: [
          {
            required: true,
            trigger: 'change',
            message: '请选择上级菜单'
          }
        ],
        menuLabel: [
          {
            required: true,
            trigger: 'change',
            message: '请填写菜单名称'
          }
        ],
        // icon: [
        //   {
        //     required: true,
        //     trigger: 'change',
        //     message: '请填写图标'
        //   }
        // ],
        name: [
          {
            required: true,
            trigger: 'change',
            message: '请填写路由名称'
          }
        ],
        path: [
          {
            required: true,
            trigger: 'change',
            message: '请填写路由地址'
          }
        ],
        url: [
          {
            required: true,
            trigger: 'change',
            message: '请填写组件路径'
          }
        ],
        menuCode: [
          {
            required: true,
            trigger: 'change',
            message: '请填写权限字段'
          }
        ]
      },
      // 新增或编辑数据绑定
      addModel: {
        editType: '',
        menuId: '',
        parentId: '',
        menuLabel: '',
        menuCode: '',
        name: '',
        path: '',
        url: '',
        type: '',
        icon: '',
        remark: '',
        parentName: '',
        orderNum: ''
      },
      // 弹框属性
      dialog: {
        title: '',
        height: 310,
        width: 660,
        visible: false
      }
    }
  },
  created() {
    this.getMenuList()
  },
  mounted() {
    this.$nextTick(() => {
      this.tableHeight = window.innerHeight - 180
    })
  },
  methods: {
    editMenu(row) {
      // 清空表单
      this.$resetForm('addForm', this.addModel)
      // 把当前编辑的数据复制到表单数据域
      this.$objCoppy(row, this.addModel)
      // 设置编辑的标识
      this.addModel.editType = '1'
      // 设置弹框属性
      this.dialog.title = '编辑菜单'
      this.dialog.visible = true
    },
    async deleteMenu(row) {
      const confirm = await this.$myconfirm('确定删除该数据吗?')
      if (confirm) {
        const res = await deleteMenuApi({ menuId: row.menuId })
        if (res && res.code === 200) {
          // 刷新表格
          this.getMenuList()
          // 删除成功提示
          this.$message.success(res.msg)
        }
      }
    },
    // 上级弹框确认
    onParentConfirm() {
      this.addModel.parentId = this.selectNode.menuId
      this.addModel.parentName = this.selectNode.menuLabel
      this.parentDialog.visible = false
    },
    // 上级弹框关闭
    onParentClose() {
      this.parentDialog.visible = false
    },
    // 上级树节点点击事件
    treeClick(node) {
      console.log(node)
      this.selectNode.menuId = node.menuId
      this.selectNode.menuLabel = node.menuLabel
    },
    // 选择上级部门点击事件
    async selectParent() {
      // 获取上级部门点击事件
      const res = await getParentTreeApi()
      if (res && res.code === 200) {
        this.parentList = res.extend.list
        console.log(this.parentList)
      }
      this.parentDialog.visible = true
    },
    // 新增或编辑弹框确认事件
    onConfirm() {
      this.$refs.addForm.validate(async(valid) => {
        if (valid) {
          let res = null
          if (this.addModel.editType === '0') {
            res = await addMenuApi(this.addModel)
          } else {
            res = await editMenuApi(this.addModel)
          }
          if (res && res.code === 200) {
            // 刷新列表
            this.getMenuList()
            this.dialog.visible = false
            this.$message.success(res.msg)
          }
        }
      })
    },
    // 新增或编辑弹框取消事件
    onClose() {
      this.dialog.visible = false
    },
    addMenu() {
      // 清空表单数据
      this.$resetForm('addForm', this.addModel)
      this.dialog.title = '新增菜单'
      this.addModel.editType = '0'
      this.dialog.visible = true
    },
    async getMenuList() {
      const res = await getMenuListApi()
      if (res && res.code === 200) {
        this.tableList = res.extend.list
      }
      console.log(res)
    }
  }
}
</script>

<style lang="less" scoped>

</style>
